﻿using Engine;
using Engine.Graphics;
using Game;

namespace Mekiasm
{
    public class ItemHFluid : Item
    {
        public Color topcolor;
        public static Texture2D Texture = ContentManager.Get<Texture2D>("Mekiasm/Textures/fluid/molten_still");
        public ItemHFluid(int d, string n, Color top) : base(d, n)
        {
            topcolor = top;
            Common.AnimateRender animateRender = new Common.AnimateRender(GetType().GetHashCode());
            animateRender.OnCreate = () => {
                animateRender.RenderTarget = new RenderTarget2D(32,32,1,ColorFormat.Rgba8888,DepthFormat.None);
            };
            animateRender.Draw = () => {
                if (Time.PeriodicEvent(1.0, 0.0)) animateRender.Frame1 += 1;
                if (animateRender.Frame1 == 4) animateRender.Frame1 = 0;
                ILibrary.CopyTextureToRenderTarget(Texture, animateRender.RenderTarget, (pr) => {
                    TexturedBatch2D textured1 = pr.TexturedBatch(Texture);
                    TexturedBatch2D textured2 = pr.TexturedBatch(Texture, true);
                    textured1.QueueQuad(Vector2.Zero, new Vector2(32f), 1f, Vector2.Zero, Vector2.One, Color.White);
                    textured2.QueueQuad(Vector2.Zero, new Vector2(32f), 0f, new Vector2(0f, 0.25f * animateRender.Frame1), new Vector2(1f, 0.25f * animateRender.Frame1 + 0.25f), topcolor);
                });
            };
            AnimateTextureSystem.Add(animateRender);
        }
        public override bool IsFaceTransparent(SubsystemTerrain subsystemTerrain, int face, int value)
        {
            return true;
        }
        public override bool IsVisible(int value)
        {
            return true;
        }
        public override bool OnUse(Ray3 aim, ComponentMiner componentMiner, TerrainRaycastResult? raycast)
        {
            return false;
        }
        public override string GetCategory(int value)
        {
            return MekiasmInit.categories_hedian;
        }
        public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
        {
            ILibrary.DrawCubeBlock(primitivesRenderer, value, new Vector3(size), ref matrix, topcolor, topcolor, environmentData, ILibrary.texture);
        }
        public override void GenerateTerrainVertices(Block block, BlockGeometryGenerator generator, TerrainGeometry geometry, int value, int x, int y, int z)
        {
            SubsystemMekBlockBehavior.mekModelGenerator.GenerateCubeVertices(block, value, x, y, z, Color.White, SubsystemMekDraw.AddDraw(x, y, z, geometry, ILibrary.texture).OpaqueSubsetsByFace);
        }
        public override void OnBlockRemoved(SubsystemBlockBehavior blockBehavior, int value, int newValue, int x, int y, int z)
        {
            SubsystemMekDraw.RemoveDraw(x, y, z);
        }
    }
}
